package blazingbubble.gui.panels;




import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import blazingbubble.gui.INavigationButton;
import blazingbubble.gui.ISwitchable;
import blazingbubble.gui.PanelRegistry;
import blazingbubble.gui.Resources;
import blazingbubble.gui.elements.MenuButton;

public class JoinGamePanel extends JPanel implements ActionListener{
	private static final long serialVersionUID = -8101015828071694326L;
	
	public static final String PANEL_NAME = "JoinGame";
		
	private ISwitchable parentFrame = null;
	
	private MenuButton joinIPButton = new MenuButton(Resources.JOINIPBUTTON_IMAGEPATH);
	private MenuButton browseButton = new MenuButton(Resources.BROWSEBUTTON_IMAGEPATH);
	private MenuButton backToMainButton = new MenuButton(Resources.BACKBUTTON_IMAGEPATH);
	
	private JoinIPPanel joinIPPanel = null;
	private BrowseGamesPanel browseGamesPanel = null;
	
	public JoinGamePanel(ISwitchable parentFrame){
		this.parentFrame = parentFrame;
		PanelRegistry.getInstance().registerPanel(PANEL_NAME, this);
		setUp();
	}
	
	private void setUp(){		
		this.setLayout(null);
		
		joinIPPanel = new JoinIPPanel(parentFrame);
		browseGamesPanel = new BrowseGamesPanel(parentFrame);
		
		joinIPButton.addActionListener(this);
		joinIPButton.setDestinationPanel(joinIPPanel);
		this.add(joinIPButton);
		joinIPButton.setBounds(45, 150, 260, 55);
		
		browseButton.addActionListener(this);
		browseButton.setDestinationPanel(browseGamesPanel);
		this.add(browseButton);
		browseButton.setBounds(45, 220, 260, 55);
		
		backToMainButton.setDestinationPanel(PanelRegistry.getInstance().lookUpPanel(MainMenuPanel.PANEL_NAME));
		backToMainButton.addActionListener(this);
		this.add(backToMainButton);
		backToMainButton.setBounds(45, 290, 260, 55);
		updateUI();
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(MainMenuPanel.BACKGROUND_IMAGE, 0, 0, getWidth(), getHeight(), this);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getSource().equals(browseButton)){
			if (!parentFrame.isLoggedIn()){
				parentFrame.getLobbyCommandFactory().getLogin().login(null);
			}
			parentFrame.switchSize(500, 500);
		}
		parentFrame.switchPanel(((INavigationButton)event.getSource()).getDestinationPanel());
	}
}
